package com.zilue.payment.gateway.web.advice;

import com.zilue.payment.gateway.reurndata.PublicReturn;
import com.zilue.payment.gateway.reurndata.enums.RequestStatusEnum;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.http.converter.HttpMessageNotReadableException;
import org.springframework.web.bind.annotation.ControllerAdvice;
import org.springframework.web.bind.annotation.ExceptionHandler;
import org.springframework.web.bind.annotation.ResponseBody;

import javax.servlet.http.HttpServletRequest;

/**
 * Created by zilue on 2017/3/31.
 */
@ControllerAdvice(basePackages = {"com.zilue.payment.gateway"})
public class ExecptionAdvice {
    private static final Logger logger = LoggerFactory.getLogger(ExecptionAdvice.class);

    @ExceptionHandler(RuntimeException.class)
    @ResponseBody
    public PublicReturn catchExecption(HttpServletRequest request, Exception e) {
        if (e instanceof HttpMessageNotReadableException) {
            logger.info("调用方请求格式不规范", e);
            PublicReturn failedReturn = PublicReturn.createFailedReturn(RequestStatusEnum.RequestBodyIllegal);
            return failedReturn;
        }else{
            logger.error("统一支付接口异常 严重", e);
            PublicReturn failedReturn = PublicReturn.createFailedReturn(RequestStatusEnum.RequestBusyness);
            return failedReturn;
        }
    }

}
